package com.koo.modules.b2b.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.koo.common.response.R;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.excel.EasyExcelUtil;
import com.koo.modules.b2b.entity.B2bStoreToOutStoreEntity;
import com.koo.modules.b2b.service.B2bStoreToOutStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * b2b门店与注册企业下原门店信息对应表
 *
 * @author charlin
 * @email 136137465@qq.com
 * @date 2023-01-05 09:53:32
 */
@Slf4j
@Api(tags = "b2b门店与注册企业下原门店信息对应表")
@RestController
@RequestMapping("third/b2b/storetooutstore")
public class B2bStoreToOutStoreController {

    @Autowired
    private B2bStoreToOutStoreService b2bStoreToOutStoreService;

    @ApiOperation("获取列表")
    @RequestMapping("/list")
    @RequiresPermissions("b2b:b2bstoretooutstore:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = b2bStoreToOutStoreService.queryPage(params);
        return R.ok().put("page", page);
    }


    @ApiOperation("根据主键查询信息")
    @RequestMapping("/info/{outId}")
    @RequiresPermissions("b2b:b2bstoretooutstore:info")
    public R info(@PathVariable("outId") String outId){
		B2bStoreToOutStoreEntity b2bStoreToOutStore = b2bStoreToOutStoreService.getById(outId);
        return R.ok().setData(b2bStoreToOutStore);
    }

    @ApiOperation("保存")
    @RequestMapping("/save")
    @RequiresPermissions("b2b:b2bstoretooutstore:save")
    public R save(@RequestBody B2bStoreToOutStoreEntity b2bStoreToOutStore){
		b2bStoreToOutStoreService.save(b2bStoreToOutStore);
        return R.ok();
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    @RequiresPermissions("b2b:b2bstoretooutstore:update")
    public R update(@RequestBody B2bStoreToOutStoreEntity b2bStoreToOutStore){
		b2bStoreToOutStoreService.updateById(b2bStoreToOutStore);
        return R.ok();
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("b2b:b2bstoretooutstore:delete")
    public R delete(@RequestBody Long[] outIds){
		b2bStoreToOutStoreService.removeByIds(Arrays.asList(outIds));
        return R.ok();
    }


    @ApiOperation("导入")
    @RequestMapping("/importData")
    @RequiresPermissions("b2b:b2bstoretooutstore:import")
    @Transactional(rollbackFor = {Exception.class})
    public R importData(@RequestParam("file") MultipartFile file) {
        try {
            // 读取文件流
            EasyExcel.read(
                    file.getInputStream(),
                    B2bStoreToOutStoreEntity.class,
                    new EasyExcelUtil(b2bStoreToOutStoreService))// 监听器
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("库存模板")
                    .doRead();
        } catch (IOException e) {
            return R.error("上传失败!");
        }
        return R.ok("上传成功!");
    }

    @ApiOperation("导出")
    @GetMapping("/exportData")
    @RequiresPermissions("b2b:b2bstoretooutstore:export")
    public void exportData(HttpServletResponse response) {
        try {
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = EasyExcelUtil.initExportParams( "b2b门店与注册企业下原门店信息对应表报表", response);
            List<B2bStoreToOutStoreEntity> list = b2bStoreToOutStoreService.list(null);
            EasyExcel.write(response.getOutputStream()
                    , B2bStoreToOutStoreEntity.class)
                    .registerWriteHandler(horizontalCellStyleStrategy) //加载样式
                    .sheet()
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
